package com.hontech.desktoppastacooker.device

import com.hontech.bv.conn.serial.Uint16
import com.hontech.desktoppastacooker.conn.serial.Uint8
import com.hontech.desktoppastacooker.utils.isSetbit
import com.hontech.desktoppastacooker.utils.toHex4

class Status(
    val appVersion: Uint16 = Uint16(),
    val sw: Uint8 = Uint8(),
    val sensor: Uint8 = Uint8(),
    val kpa: Uint16 = Uint16(),
    val steamTemp: Uint16 = Uint16(),
    val flowCount: Uint16 = Uint16(),
    val adc: Uint16 = Uint16(),
    val parts: Uint16 = Uint16(),
    val draw: Uint8 = Uint8(),
    val heat: Uint8 = Uint8()
) {

    fun versionString(): String {
        return appVersion.value.toHex4()
    }

    fun isNozzleTop() = sw.value.isSetbit(0)
    fun isDoorFront() = sw.value.isSetbit(1)
    fun isDoorBack() = sw.value.isSetbit(2)
    fun isNear() = sw.value.isSetbit(3)

    fun isLeak() = sensor.value.isSetbit(0)
    fun isSteamHigh() = sensor.value.isSetbit(1)
    fun isSteamLow() = sensor.value.isSetbit(2)
    fun isSteamOk() = sensor.value.isSetbit(3)
    fun isNozzleAlm() = sensor.value.isSetbit(4)

    fun kpa() = kpa.signalValue()
    fun steam() = steamTemp.signalValue()

    fun isSteamHeatOpen() = parts.value.isSetbit(0)
    fun isWaterHeatOpen() = parts.value.isSetbit(1)
    fun isSteamPumpOpen() = parts.value.isSetbit(2)
    fun isWaterPumpOpen() = parts.value.isSetbit(3)
    fun isValve1Open() = parts.value.isSetbit(4)
    fun isValve2Open() = parts.value.isSetbit(5)
    fun isValve3Open() = parts.value.isSetbit(6)
    fun isValve4Open() = parts.value.isSetbit(7)

    fun isOutletPumpOpen() = parts.value.isSetbit(8)

    fun drawState(): String {
        return when(draw.value) {
            0 -> "抽水"
            1 -> "空闲"
            2 -> "停止"
            3 -> "超时"
            4 -> "抽废水"
            else -> "未知"
        }
    }

    fun heatState(): String {
        return when(heat.value) {
            0 -> "空闲"
            1 -> "保温"
            2 -> "超时"
            3 -> "温度传感器异常"
            4 -> "停止"
            else -> "未知状态"
        }
    }


}
